package com.llp.javaweb.servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.llp.javaweb.dao.LoginDao;
import com.llp.javaweb.pojo.User;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author llp
 * {@code @date} 2022年11月01日17:12
 */
@WebServlet("/user/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user=new User(username,password);
        User theUser = LoginDao.getUser(user);
        // System.out.println(theUser);
        //如果存在该用户，跳转到用户管理，否则跳转到主页
        if(theUser!=null)
        {
            HttpSession session = req.getSession();
            session.setAttribute("user",theUser);
            req.getRequestDispatcher("/user/selectAllUser").forward(req,resp);

        }
             else resp.sendRedirect("/");
    }
}
